<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Gallery extends MX_Controller {

	var $_table = 'gallery';

	function __construct() {
		parent::__construct ();
		if (! isset ( $this->session->userdata ['admin'] )) {
			redirect ( 'admin/login' );
		}

		$_SESSION ['KCFINDER'] = array ();
		$_SESSION ['KCFINDER'] ['disabled'] = false;

		$this->load->library ( 'ckeditor', array ('instanceName' => 'CKEDITOR1', 'basePath' => base_url () . "ckeditor/", 'outPut' => true ) );
		$this->load->model ( 'gallery_model' );
	}

	function detail() {
		$id = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		$detail = $this->gallery_model->getDetail ( $id );
		$data ['detail'] = $detail;
		$data ['content'] = 'admin/detail';
		$this->load->view ( 'back_end/index', $data );
	}

	function add() {
		if ($this->input->post ()) {
			$data = array ();
			$image_data=$this->gallery_model->do_upload();
			$data ['file_name'] = ( string ) $image_data['file_name'];
			$data ['type'] = ( string ) $this->input->post ( 'type' );
			$data ['path'] = '/storage/images';
			$this->gallery_model->saveItem ( 'gallery', array ('field' => 'id', 'id' => 0 ), $data );
			redirect ( 'admin/gallery/listgallery/'.$this->input->post ( 'type' ) );
		}
	}

	function listgallery() {
		$type = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		$gallery = $this->gallery_model->getList ($type);
		$data ['list'] = $gallery;
		$data ['type'] = $type;
		$data ['content'] = 'admin/list';
		$this->load->view ( 'back_end/index', $data );
	}

	function listalbum() {
		$gallery = $this->gallery_model->getListAlbum();
		$data ['list'] = $gallery;
		$data ['content'] = 'admin/listalbum';
		$this->load->view ( 'back_end/index', $data );
	}
	function addalbum() {
		if ($this->input->post ()) {
			$data = array ();
			$data ['name'] = ( string ) $this->input->post ( 'name' );
			$albumid=$this->gallery_model->saveItem ( 'album', array ('field' => 'id', 'id' => 0 ), $data );
			redirect ( 'admin/gallery/listphotos/'.$albumid );
		}
		$data ['action'] = 'addalbum';
		$data ['content'] = 'admin/addalbum';
		$this->load->view ( 'back_end/index', $data );
	}
	function delalbum(){
		$id = ( string ) $this->uri->segment ( 4 );
		$this->gallery_model->delalbum ( $id );
		redirect ( 'admin/gallery/listalbum');
	}

	function listphotos(){
		$albumid = ( string ) $this->uri->segment ( 4 );
		$lists=$this->gallery_model->getListPhotos($albumid);
		$data ['lists'] = $lists;
		$data ['content'] = 'admin/listphotos';
		$this->load->view ( 'back_end/index', $data );
	}

	function addphotos(){
		$albumid = $this->input->post ( 'albumid' );
		if (isset($_FILES['img']) && $_FILES['img']['name']!=''){
			$data_img = array ();
			$image_data = $this->gallery_model->do_upload ();

			$data_img ['file_name'] = ( string ) $image_data ['file_name'];
			$data_img ['path'] = './storage/images';
			$data_img ['albumid'] = $albumid;
			$img_id=$this->gallery_model->saveItem ( 'photos', array ('field' => 'id', 'id' => 0 ), $data_img );
			redirect ( 'admin/gallery/listphotos/'.$albumid );
		}
	}

	function delphoto() {
		$id = ( int ) $this->uri->segment ( 4 );
		$albumid = ( string ) $this->uri->segment ( 5 );
		$this->gallery_model->delphoto ( $id );
		redirect ( 'admin/gallery/listphotos/'.$albumid );
	}

	function del($id = NULL) {
		$type = ( string ) $this->uri->segment ( 4 );
		$id = ( int ) $this->uri->segment ( 5 );
		$this->gallery_model->del ( $id );
		redirect ( 'admin/gallery/listgallery/'.$type );
	}

	function saveActive(){
		$type=$_REQUEST['type'];
		$id=$_REQUEST['id'];
		$value=$_REQUEST['value'];
		$query1 = $this->db->query("UPDATE `ci_gallery` SET `active`='$value' WHERE (`id`='$id')");
		if($type == 'Logo' && $value==1){
			$query2 = $this->db->query("UPDATE `ci_gallery` SET `active`='0' WHERE (`id`<>'$id' AND `type` = '$type')");
		}
	}
	
	function addHomepage()
	{
		$id=$_REQUEST['id'];
		$value=$_REQUEST['value'];
		$query1 = $this->db->query("UPDATE `ci_video` SET `homepage`='$value' WHERE (`id`='$id')");		
		$query2 = $this->db->query("UPDATE `ci_video` SET `homepage`='0' WHERE (`id`<>'$id')");
		
	}

	function listvideo(){
	$data['dulieu_ve_video'] = $this->gallery_model->getlistvideo();
	$data ['content'] = 'admin/listvideo';
    $this->load->view ( 'back_end/index', $data );
	}

	function addvideo() {
	$data ['content'] = 'admin/addvideo';
    $this->load->view ( 'back_end/index', $data );
    }

	function uploadvideo() {	                 
		$ten = $this->input->post('ten_video');
		$data['ten_dat'] = $ten;
		$data['ten_file_video'] = $this->input->post('ten_file_video');
		$data['homepage'] = '0';
        //echo "Successfully Uploaded";
		$this->gallery_model->save_video_db($data);
		redirect ( 'admin/gallery/listvideo');	           		
	}

	function xoavideo() {
		$id_video_xoa = $this->uri->segment(4);		
	   $this->db->where('id', $id_video_xoa);
	   $this->db->delete('ci_video');
	    redirect('admin/gallery/listvideo');

	}

}